<template>
    <el-dialog :title="!dataForm.roleId ? '新增' : '修改'" destroy-on-close :before-close="closeDailog"
        :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="所属集团" prop="pkGroup">
                <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                </ec-group-tree-select>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <ec-org-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg"
                    placeholder="请选择所属组织"></ec-org-tree-select>
            </el-form-item>
            <el-form-item label="角色标识" prop="roleName">
                <el-input v-model="dataForm.roleName" placeholder="角色标识(英文字符串)" :disabled="dataForm.roleId != 0">
                </el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDescription">
                <el-input v-model="dataForm.roleDescription" placeholder="角色描述"></el-input>
            </el-form-item>
            <el-form-item label="角色状态" prop="roleStatus">
                <ec-dict-select v-model="dataForm.roleStatus" placeholder="请选择角色状态" dictType="use_status">
                </ec-dict-select>
            </el-form-item>
            <!-- <el-form-item label="数据权限">
                <tree-transfer :title="title" ref="tree" :from_data='fromData' :to_data='toData' @addBtn='add'
                    @removeBtn='remove' :mode='mode' height='340px' filter openAll arrayToTree :check-strictly="true">
                </tree-transfer>
            </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import treeTransfer from 'el-tree-transfer' // 引入
    import * as roleService from '@/api/upms/role'
    import * as dictService from '@/api/bdms/dict'
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            return {
                title: ['未选择部门节点列表', '已选择部门节点列表'],
                mode: "transfer",
                visible: false,
                submitLoading: false,
                dataForm: {
                    roleId: 0,
                    pkGroup: '',
                    pkOrg: '',
                    roleName: '',
                    roleType: 'bus',
                    roleDescription: '',
                    roleStatus: 'Y',
                    revision: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    roleName: [{
                        required: true,
                        message: '角色标识不能为空',
                        trigger: 'blur'
                    }],
                    roleDescription: [{
                        required: true,
                        message: '角色描述不能为空',
                        trigger: 'blur'
                    }],
                    roleStatus: [{
                        required: true,
                        message: '角色状态不能为空',
                        trigger: 'blur'
                    }],
                },
                fromData: [],
                toData: [],
                ids: [],
            }
        },
        components: {
            treeTransfer
        }, // 注册
        methods: {
            init(id) {
                this.dataForm.roleId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.roleId) {
                        roleService.getroleinfo(this.dataForm.roleId).then(data => {
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.roleName = data.roleName
                            this.dataForm.roleType = data.roleType
                            this.dataForm.roleDescription = data.roleDescription
                            this.dataForm.roleStatus = data.roleStatus
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'roleId': this.dataForm.roleId || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'roleName': this.dataForm.roleName,
                            'roleType': this.dataForm.roleType,
                            'roleDescription': this.dataForm.roleDescription,
                            'roleStatus': this.dataForm.roleStatus,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.roleId ? roleService.addsysrole(params) : roleService
                            .updaterole(
                                params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            // 监听穿梭框组件添加
            add(fromData, toData, obj) {
                let ids = obj.keys.concat(obj.harfKeys)
                this.setIds('add', ids)
            },
            // 监听穿梭框组件移除
            remove(fromData, toData, obj) {
                let ids = obj.keys
                this.setIds('remove', ids)
            },
            // ids数组操作
            setIds(type, objs) {
                if (type === 'add') {
                    // 数据合并去重
                    objs.forEach(item => {
                        if (item !== null) {
                            this.ids.push(item)
                        }
                    })
                    this.ids = Array.from(new Set(this.ids))
                }
                if (type === 'remove') {
                    this.arrSub(this.ids, objs)
                }
            },
            // 数组相减
            arrSub(a, b) {
                for (var i = 0; i < b.length; i++) {
                    for (var j = 0; j < a.length; j++) {
                        if (a[j] == b[i]) {
                            a.splice(j, 1);
                            j = j - 1;
                        }
                    }
                }
                return a;
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>